package com.pcafs.manage.controller;

import com.pcafs.common.annotation.Log;
import com.pcafs.common.config.PcafsConfig;
import com.pcafs.common.core.controller.BaseController;
import com.pcafs.common.core.domain.AjaxResult;
import com.pcafs.common.core.page.TableDataInfo;
import com.pcafs.common.enums.BusinessType;
import com.pcafs.common.utils.file.FileUploadUtils;
import com.pcafs.common.utils.poi.ExcelUtil;
import com.pcafs.manage.bo.StudentInfoAddBo;
import com.pcafs.manage.bo.StudentInfoEditBo;
import com.pcafs.manage.bo.StudentInfoQueryBo;
import com.pcafs.manage.domain.StudentInfo;
import com.pcafs.manage.service.IStudentInfoService;
import com.pcafs.manage.vo.StuStuDetail;
import com.pcafs.manage.vo.StudentInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 学生信息Controller
 *
 * @author pcafs
 * @date 2021-05-01
 */
@Api(value = "学生信息控制器", tags = {"学生信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/manage/stuInfo")
public class StudentInfoController extends BaseController {

    private final IStudentInfoService iStudentInfoService;


    @ApiOperation("查询学生信息列表")
    @GetMapping("/test")
    public TableDataInfo<StudentInfoVo> test() {
        return null;
    }

    /**
     * 查询学生信息列表
     */
    @ApiOperation("查询学生信息列表")
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:list')")
    @GetMapping("/list")
    public TableDataInfo<StudentInfoVo> list(StudentInfoQueryBo bo) {
        startPage();
        List<StudentInfoVo> list = iStudentInfoService.queryList(bo);
        return getDataTable(list);
    }


    @ApiOperation("查询学生信息列表")
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:list')")
    @GetMapping("/listVo")
    public TableDataInfo<StudentInfoVo> listVo(StudentInfo studentInfo) {
        startPage();
        List<StudentInfoVo> list = iStudentInfoService.queryListVo(studentInfo);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @ApiOperation("导出学生信息列表")
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<StudentInfoVo> export(StudentInfoQueryBo bo) {
        List<StudentInfoVo> list = iStudentInfoService.queryList(bo);
        ExcelUtil<StudentInfoVo> util = new ExcelUtil<StudentInfoVo>(StudentInfoVo.class);
        return util.exportExcel(list, "学生信息");
    }

    /**
     * 获取学生信息详细信息
     */
    @ApiOperation("获取学生信息详细信息")
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:query')")
    @GetMapping("/{id}")
    public AjaxResult<StudentInfoVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iStudentInfoService.queryById(id));
    }

    /**
     * 获取学生 详情
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:query')")
    @GetMapping("/getStuDetail/{id}")
    public AjaxResult<StuStuDetail> getStuDetail(@PathVariable("id") Long id) {
        return AjaxResult.success(iStudentInfoService.selectStuDetail(id));
    }

    /**
     * 新增学生信息
     */
    @ApiOperation("新增学生信息")
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody StudentInfoAddBo bo) {
        return toAjax(iStudentInfoService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改学生信息
     */
    @ApiOperation("修改学生信息")
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody StudentInfoEditBo bo) {
        return toAjax(iStudentInfoService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除学生信息
     */
    @ApiOperation("删除学生信息")
    @PreAuthorize("@ss.hasPermi('manage:stuinfo:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iStudentInfoService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @Log(title = "学生头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file, @RequestParam("stuId") Long stuId) throws IOException {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(PcafsConfig.getAvatarPath(), file);
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setId(stuId);
            studentInfo.setAvatar(avatar);
            if (iStudentInfoService.updateById(studentInfo)) {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
